package world.criver.mm_vworld_server.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import world.criver.mm_vworld_server.config.conConfig;
import world.criver.mm_vworld_server.data.daAdmin;
import world.criver.mm_vworld_server.enums.enumKey;
import world.criver.mm_vworld_server.function.funAdminMessage;
import world.criver.mm_vworld_server.function.funAdminVerifyCode;

import java.time.LocalDateTime;

@Service
public class serAdminLogin {
    @Autowired
    private funAdminMessage funAdminMessage;
    @Autowired
    private funAdminVerifyCode funAdminVerifyCode;
    @Autowired
    private conConfig conConfig;

    private ObjectMapper om=new ObjectMapper();

    public ObjectNode login(JsonNode info){
        if(!info.has(enumKey.password.name()))return funAdminMessage.error("请输入密码");
        String password=info.get(enumKey.password.name()).asText();
        if(!conConfig.getAdminPassword().equals(password))return funAdminMessage.error("密码错误");
        daAdmin.verifyCode=funAdminVerifyCode.createCode();
        daAdmin.loginDateTime= LocalDateTime.now();
        ObjectNode on=om.createObjectNode();
        on.put(enumKey.tip.name(), "登陆成功");
        on.put(enumKey.verifyCode.name(), daAdmin.verifyCode);
        on.put(enumKey.status.name(), true);
        on.put(enumKey.timestamp.name(), System.currentTimeMillis());
        return on;
    }
}
